package org.ticlib.navigator
{
	import flash.events.EventDispatcher;
	
	import mx.managers.IBrowserManager;
	import mx.utils.URLUtil;

	public class UrlManager extends EventDispatcher
	{
		public static const FRAGMENT_SEPRATOR:String = "&";
		public static const PAGE_QUERY_STRING:String = "__pagename";

		private var browserManager:IBrowserManager;
		
		public function UrlManager(browserMan:IBrowserManager)
		{
			browserManager = browserMan;
		}

		public function getObjectFromUrl():Object
		{
			var fragment:String = browserManager.fragment;
			return fragment == null ? null : URLUtil.stringToObject(fragment, 
				FRAGMENT_SEPRATOR);
		}
		
		/**
		 * melakukan penulisan parameter objek ke url
		 * */
		public function writeObjectToUrl(name:String, title:String, params:Object):void
		{
			if(params == null) params = new Object();
			params[PAGE_QUERY_STRING] = name;
			browserManager.setTitle(title);
			try
			{
				browserManager.setFragment(URLUtil.objectToString(params, 
					FRAGMENT_SEPRATOR));
			}
			catch(e:Error)
			{
				throw new Error("Problem writing page parameters to url, a parameter must be a primitive type");
			}
		}
	}
}